package com.aim.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.aim.api.AIMCaseConstants;

public class FileUploader {
	public boolean doFileUpload(String exsistingFileName, String caseNumber) {
		Context systemContext=WebServiceUtil.getContext();
		boolean uploadResult=true;
		SharedPreferences preferences = systemContext.getSharedPreferences(Preferences.NAME, 0);
		String userName=preferences.getString(AIMCaseConstants.USERNAME, "sysadmin");
		String password=preferences.getString(AIMCaseConstants.PASSWORD, "demoapps");
		//String contextURL=preferences.getString(AIMCaseConstants.CONTEXTURL, "http://nobelserver.com:9080/aim");
		//Log.e("eee",contextURL.toString());
		String validateCode=preferences.getString(AIMCaseConstants.VALIDATECODE, "nobel");
	
		HttpURLConnection conn = null;
		DataOutputStream dos = null;
		DataInputStream inStream = null;

		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary = "******";

		int bytesRead, bytesAvailable, bufferSize;

		byte[] buffer;

		int maxBufferSize = 1 * 1024 * 1024;
		String responseFromServer = "";
		String urlString ="http://demoapps.nobelsystems.net/aim/uploadwebform.aspx";
		Log.e("before upload try","before");
		try {
			FileInputStream fileInputStream = new FileInputStream(new File(
					exsistingFileName));
			URL url = new URL(urlString);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);

			dos = new DataOutputStream(conn.getOutputStream());
			
			
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"caseNumber\""
					+ lineEnd + lineEnd);
			dos.writeBytes(caseNumber + lineEnd);
			
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"userName\""
					+ lineEnd + lineEnd);
			dos.writeBytes(userName + lineEnd);
			
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"password\""
					+ lineEnd + lineEnd);
			dos.writeBytes(password + lineEnd);
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"validateCode\""
					+ lineEnd + lineEnd);
			dos.writeBytes(validateCode + lineEnd);
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"fileFile\";filename=\""
					+ exsistingFileName + "\"" + lineEnd);
			dos.writeBytes(lineEnd);
			
			
			
			bytesAvailable = fileInputStream.available();
			bufferSize = Math.min(bytesAvailable, maxBufferSize);
			buffer = new byte[bufferSize];
			bytesRead = fileInputStream.read(buffer, 0, bufferSize);
			Log.e("in try","12");
			while (bytesRead > 0) {
				dos.write(buffer, 0, bufferSize);
				bytesAvailable = fileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				bytesRead = fileInputStream.read(buffer, 0, bufferSize);
			}

			dos.writeBytes(lineEnd);
			dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
			fileInputStream.close();
			dos.flush();
			dos.close();

		} catch (MalformedURLException ex) {
		
			Log.e("Upload", "Upload",ex);
			uploadResult=false;
		}

		catch (Exception ioe) {
			Log.e("Upload", "Upload",ioe);
			uploadResult=false;
		}

		try {
			inStream = new DataInputStream(conn.getInputStream());
			String str= inStream.readLine();
			Log.e("suces","0"+str+"0");
			if(str.trim().equals("Successfulsuccess!")||str.length()==0)
			{
				uploadResult=true;
				File imageFile=new File(exsistingFileName);
//				imageFile.delete();
				
			}
			else
			{
				uploadResult=false;
			}
			inStream.close();

		} catch (Exception ioex) {
			ioex.printStackTrace();
			uploadResult=false;
		}
		return uploadResult;

	}
//
//	public static void main(String args[]) {
//		FileUploader loader = new FileUploader();
//		loader.doFileUpload("d:\\bg-test-large.png", "79179");
//	}
}
